// 购物车数据action
import { get, post } from '@common/ajax';
import urls from '@common/url';
import logger from '@common/logger';
import { goodsAddProps, goodsAddSkus } from '@biztools/shop';
import { getGoodsProdsList } from '@biztools/shopDetail';
import {
    SET_GOODS_DETAIL_INFO,
    GET_GOODS_DETAIL_INFO,
    SET_SELECT_SKU_INFO,
    SET_GOODS_WAREHOUSE_INFO,
} from '../mutationType';
const shopDetail = {
    state: {
        goodsDetail: null, // 商品详情
        selectSkuInfo: null, // 选中的规格信息
        goodsWarehouseInfo: null, // 商品相关仓库信息
    },
    mutations: {
        GET_GOODS_DETAIL_INFO: (state, goodsDetail) => {
            Object.assign(state, { goodsDetail });
        },
        SET_GOODS_DETAIL_INFO: (state, goodsDetail) => {
            Object.assign(state, { goodsDetail });
        },
        GET_GOODS_PROPS_INFO: (state, goodsPropsList) => {
            Object.assign(state, { goodsPropsList });
        },
        GET_GOODS_SKUS_INFO: (state, goodsSkusList) => {
            Object.assign(state, { goodsSkusList });
        },
        SET_SELECT_SKU_INFO: (state, selectSkuInfo) => {
            Object.assign(state, { selectSkuInfo });
        },
        SET_GOODS_WAREHOUSE_INFO: (state, goodsWarehouseInfo) => {
            Object.assign(state, { goodsWarehouseInfo });
        },
    },
    actions: {
        // 获取商品信息
        getGoodsInfo: async ({ commit }, goodsId) => {
            const res = await get();
            commit(GET_GOODS_DETAIL_INFO, res);
        },
        // 设置商品信息
        setGoodsDetailInfo: ({ commit }, info) => {
            commit(SET_GOODS_DETAIL_INFO, info);
        },
        // 获取商品属性
        getGoodsPropsInfo: async ({ commit, state }) => {
            const { goodsDetail } = state;
            logger.info('ShopDetail => 获取商品属性');
            const propsInfo = await get(urls.shopGoodsProps, {
                prod_id: goodsDetail.id,
                detail_required: 1, // 默认全部返回详情
            });
            logger.info('Detail => 获取的属性信息为', propsInfo);
            const _goodsDetail = goodsAddProps(goodsDetail, propsInfo);
            logger.info('Detail => 最后商品的属性为', _goodsDetail);
            commit(SET_GOODS_DETAIL_INFO, _goodsDetail);
        },
        // 获取商品规格库存
        getGoodsSkusInfo: async ({ commit, state }, { warehouseId, storeId }) => {
            const { goodsDetail } = state;
            const prods = getGoodsProdsList(goodsDetail);
            let params = {
                prods,
            };
            if (warehouseId) params = {
                ...params,
                warehouse_id: warehouseId,
            };
            if (storeId) params = {
                ...params,
                store_id: storeId,
            };
            const res = await post(urls.shopGoodsSku, params);
            const list = res.prods;
            logger.info('Detail => 获取的规格为', list);
            if (list.length > 0) {
                const _goodsDetail = goodsAddSkus(goodsDetail, list);
                logger.info('Detail => 最后商品的规格属性为', _goodsDetail);
                commit(SET_GOODS_DETAIL_INFO, _goodsDetail);
            }
        },
        // 选中规格
        // setSelectSkuInfo: ({ commit }, skuInfo) => {
        //     commit(SET_SELECT_SKU_INFO, skuInfo);
        // },
        // 获取商品的所有属性，方便页面获取选择商品添加购物车
        getGoodsAllInfo: async ({ commit }, { detail, warehouseId, storeId }) => {
            // 先获取props
            const goodsProps = await get(urls.shopGoodsProps, {
                prod_id: detail.id,
                detail_required: 1, // 默认全部返回详情
            });
            logger.success('ShopDetail => 获取的商品属性详情为：', goodsProps);
            const tempDetail = goodsAddProps(detail, goodsProps);
            // 再获取规格库存
            const prods = getGoodsProdsList(detail);
            let params = {
                prods,
            };
            if (warehouseId) params = {
                ...params,
                warehouse_id: warehouseId,
            };
            if (storeId) params = {
                ...params,
                store_id: storeId,
            };
            const skuInfo = await post(urls.shopGoodsSku, params);
            logger.success('ShopDetail => 获取的商品规格库存信息为：', skuInfo);
            // 返回拼装后的信息
            const goodsDetail = goodsAddSkus(tempDetail, skuInfo.prods);
            return goodsDetail;
        },
        // 根据地址重新匹配仓库
        matchGoodsWarehouse: async ({ state, dispatch }, { addressInfo, storeId, isMain }) => {
            try {
                const info = await get(urls.libraryInfo, {
                    lat: addressInfo.latitude,
                    lng: addressInfo.longitude,
                    city_code: addressInfo.city_code,
                    // 是否总部优先
                    hq_first: 1,
                    store_id: storeId,
                });
                logger.info('ShopDetail => 重新匹配仓库信息', info);
                dispatch('setGoodsWarehouseInfo', info);
            } catch (error) {
                dispatch('setGoodsWarehouseInfo', null);
            }
        },
        // 设置商品仓库信息
        setGoodsWarehouseInfo: ({ commit }, info) => {
            commit(SET_GOODS_WAREHOUSE_INFO, info);
        },
    },
};

export default shopDetail;
